package org.zjx.app.action.battle;

import lombok.Getter;
import org.zjx.analysis.analyze.battle.BattleAnalysis;
import org.zjx.app.commnad.ICommand;
import org.zjx.app.commnad.impl.BattleCommand;
import org.zjx.window.action.Action;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.Random;

public class BattleAction extends Action {
    @Getter
    private BattleAnalysis battleAnalysis;

    public BattleAction(IWindow targetWindow) {
        super(targetWindow, false);
        analyse();
    }

    private void analyse() {
        battleAnalysis = new BattleAnalysis(targetWindow.getScreenshot());
    }

    @Override
    protected void doExecute() throws Exception {
        ICommand command = stateManager.getGameState().getCommand();
        if (!(command instanceof BattleCommand)) return;
        if (((BattleCommand) command).getOperate().equals("物理")) {
            if (((BattleCommand) command).getDescribe().equals("普通攻击")) {
                normalAttack(((BattleCommand) command).getIndex());
            }
        }

        System.out.println("执行战斗逻辑");
    }

    //普通攻击
    public void normalAttack(int position) {
        Rectangle rectangle = battleAnalysis.getOtherSideChooseArea(position);
        if (battleAnalysis.isCharacterChoose(true, position)) {
            targetWindow.getExecutor().click(1);
        } else {
            Point point = getRandomPointInRectangle(rectangle);
            targetWindow.getExecutor().gameMove(point.x, point.y);
        }
    }

    private Point getRandomPointInRectangle(Rectangle rect) {
        Random random = new Random();
//        int x = rect.x + random.nextInt(rect.width);
        int x = rect.x + rect.width / 2;
        int y = rect.y + random.nextInt(rect.height);
        return new Point(x, y);
    }

}
